package com.xmmj.baselibrary.base.fragment

import com.xmmj.baselibrary.base.activity.mvvm.IBaseView
import com.xmmj.baselibrary.widget.loading.LoadingDialog
import android.widget.TextView
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import com.xmmj.baselibrary.R
import android.widget.Toast
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import java.lang.RuntimeException

/**
 * 实现公共UI操作的Fragment
 */
open class BaseFragment : LibBaseFragment(), IBaseView {
    //loading
    private var mDialog: LoadingDialog? = null

    //错误提示
    private var vBaseError: View? = null
    private var tvBaseError: TextView? = null
    private var btnBaseError: Button? = null

    @SuppressLint("InflateParams")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (view is ViewGroup) {
            if (vBaseError == null) {
                vBaseError = layoutInflater.inflate(R.layout.include_base_error, null)
                tvBaseError = vBaseError!!.findViewById(R.id.tv_base_error)
                btnBaseError = vBaseError!!.findViewById(R.id.btn_base_error)
                btnBaseError!!.setOnClickListener {
                    onRequest()
                    onLazyLoad()
                }
                vBaseError!!.visibility = View.GONE
                view.addView(
                    vBaseError,
                    ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                    )
                )
            }
        } else {
            throw RuntimeException("Fragment的根布局必须是一个容器布局")
        }

        onRequest()
    }

    override fun onDestroy() {
        super.onDestroy()
        hideLoading()
    }

    /**
     * 显示Loading
     */
    override fun showLoading(cancelable: Boolean) {
        hideLoading()
        if (mDialog == null) {
            mDialog = LoadingDialog()
        }
        mDialog!!.isCancelable = cancelable
        mDialog!!.show(this)
    }

    /**
     * 隐藏Loading
     */
    override fun hideLoading() {
        if (mDialog != null) {
            mDialog!!.dismiss()
            mDialog = null
        }
    }

    /**
     * 吐司
     */
    override fun toast(msg: String?) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
    }

    /**
     * 重新登录
     */
    override fun onReLogin() {

    }

    /**
     * 发起请求
     */
    override fun onRequest() {
        if (vBaseError != null) {
            vBaseError!!.visibility = View.GONE
        }
    }

    /**
     * 显示错误提示
     */
    override fun showError(imageId: Int, errorText: String?, btnText: String?) {
        if (vBaseError != null) {
            vBaseError!!.visibility = View.VISIBLE
            tvBaseError!!.visibility = View.VISIBLE
            btnBaseError!!.visibility = View.VISIBLE
            tvBaseError!!.setCompoundDrawablesWithIntrinsicBounds(0, imageId, 0, 0)
            tvBaseError!!.text = errorText
            btnBaseError!!.text = btnText
        }
    }

    /**
     * 显示空提示
     */
    override fun showEmpty(isShow: Boolean, imageId: Int, hintText: String?) {
        if (vBaseError != null) {
            vBaseError!!.visibility = if (isShow) View.VISIBLE else View.GONE
            btnBaseError!!.visibility = View.GONE
            tvBaseError!!.setCompoundDrawablesWithIntrinsicBounds(0, imageId, 0, 0)
            tvBaseError!!.text = hintText
        }
    }

    /**
     * 结束刷新动画
     */
    override fun finishRefresh() {
        val refresh = view?.let { findView(it, SmartRefreshLayout::class.java) }
        refresh?.finishRefresh()
    }

    /**
     * 结束加载更多动画
     */
    override fun finishLoadMore(noMoreData: Boolean) {
        val refresh = view?.let { findView(it, SmartRefreshLayout::class.java) }
        if (noMoreData) {
            refresh?.finishLoadMoreWithNoMoreData()
        } else {
            refresh?.setNoMoreData(false)
            refresh?.finishLoadMore()
        }
    }

    /**
     * 查找容器中指定类型的控件
     */
    private fun <T : View?> findView(view: View, cls: Class<T>): T? {
        if (view.javaClass == cls) {
            return view as T
        }
        if (view is ViewGroup) {
            for (index in 0..view.childCount) {
                if (view.getChildAt(index) != null) {
                    if (findView(view.getChildAt(index), cls) != null) {
                        return findView(view.getChildAt(index), cls)
                    }
                }
            }
        }
        return null
    }
}